requirejs.config({
    baseUrl: '../js/libs',
    paths: {
        ztree:'../plugins/ztree/js/jquery.ztree.core',
        ztreeCheck:'../plugins/ztree/js/jquery.ztree.excheck.min',
        ztreeHide:'../plugins/ztree/js/jquery.ztree.exhide'
    },
    waitSeconds : 15,
	map : {
		'*' : {
			'css' : 'css'
		}
	},
    shim: { //引入没有使用requirejs模块写法的类库。backbone依赖underscore
        'ztreeCheck':['css!../plugins/ztree/css/zTreeStyle/zTreeStyle.css'],
        'ztreeHide':['ztree']
    }
});
define([ 'jquery', 'jquery_route', 'arttemplateUtil' ,'common','config','pageJs','validform','ztree','ztreeCheck','ztreeHide','wdatePicker' ], function($,
		jquery_route, arttemplateUtil,common,config,pageJs  ) {
	$.controller("familyController", function($model, $tplHtml, $defobj,
			$modelobj, $paramobj, $this) {
		//渲染到页面方法
		$model($defobj);
		var zTree, rMenu;
		
		var setting = {
			data: {
				key: {
					url:null,
					name:"name"
				},
				simpleData: {
					enable: true,
					idKey: "id",
					pIdKey: "pid",
					rootPId: 0
				}
			},
			callback: {
				onRightClick: zTreeOnRightClick,
				onClick: zTreeOnClick
			}
		};
		function zTreeOnClick(event, treeId, treeNode) {
			$('#pid').val(treeNode.pid);
			$('#parentname').val(treeNode.getParentNode().name);
			$('#id').val(treeNode.id);
			$('#person_name').val(treeNode.name);
			/////$('#rolefrm input[type=button]').show();
		};
		function zTreeOnRightClick(event, treeId, treeNode) {
			if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
				zTree.cancelSelectedNode();
				showRMenu("root", event.clientX, event.clientY);
			} else if (treeNode && !treeNode.noR) {
				zTree.selectNode(treeNode);
				showRMenu("node", event.clientX, event.clientY);
			}
			return;
		}

		function onBodyMouseDown(event){
			if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
				rMenu.css({"visibility" : "hidden"});
			}
		}
		
		function showRMenu(type, x, y) {
			$("#rMenu ul").show();

            y += document.body.scrollTop;
            x += document.body.scrollLeft;
            rMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});

			$("body").bind("mousedown", onBodyMouseDown);
		}
		function hideRMenu() {
			if (rMenu) rMenu.css({"visibility": "hidden"});
			$("body").unbind("mousedown", onBodyMouseDown);
		}
		var zNodes =[];
		
		var loadztree=function(){
			$('#rolefrm input[type=button]').hide();
			common.ajax_request2('post','/topic/ajax/getFamilyTree',{},function($data,$code){
				zNodes=$data;
			},false);
			zTree = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
			zTree.expandAll(false);
			
			rMenu = $("#rMenu");
		};
		loadztree();
		
		$('#rolefrm').off('click','input[type=button]').on('click','input[type=button]',function(){
			common.ajax_request2('post','/topic/ajax/saveFamilyPerson', $('#rolefrm').serializeArray(),function($data,$code,$business){
				if($code==200){
					$('#rolefrm')[0].reset();
					window.layer.msg("操作成功!");
					loadztree();
				}else{
					window.layer.msg($business.businessNote);
				}
				layer.closeAll('loading'); //关闭加载层
			});
		});
		
		//鼠标经过移开
		/*$(document).off('mouseenter','.logbox .memolink').on('mouseenter','.logbox .memolink',function(){
			$(this).popover('show');
		});
		$(document).off('mouseleave','.logbox .memolink').on('mouseleave','.logbox .memolink',function(){
			$(this).popover('hide');
		});*/
	});
});